package com.sky.service.impl;

import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.IWorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class WorkSpaceServiceImpl implements IWorkSpaceService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime localDateTimeBegin, LocalDateTime localDateTimeEnd) {
        //先封装Map 集合
        Map<String,Object> map=new HashMap<>();
        map.put("begin",localDateTimeBegin);
        map.put("end",localDateTimeEnd);
        //查询总订单数
        Integer totalOrderCount= orderMapper.countByMap(map);
        //查询营业额
        Double turnover=orderMapper.sumByMap(map);
        turnover=turnover==null ? 0.0 : turnover;
        //添加完成订单的状态
        map.put("status",5);
        //查询当日状态已经完成的订单数
        Integer validOrderCount=orderMapper.countByMap(map);
        //平均客单价c
        Double unitPrice=0.0;
        //订单完成率
        Double orderCompleteRate=0.0;
        if (totalOrderCount!=0&&validOrderCount!=0){
           orderCompleteRate= validOrderCount.doubleValue()/totalOrderCount;
           unitPrice=turnover/validOrderCount;
        }
        //获取新用户数newUser
        Integer newUsers = userMapper.countByMap(map);
        return BusinessDataVO.builder()
                .turnover(turnover)
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompleteRate)
                .build();
    }

    @Override
    public SetmealOverViewVO getOverViewSetmeals() {
        return SetmealOverViewVO
                .builder()
                .sold(setmealMapper.getSold())
                .discontinued(setmealMapper.getDiscontinued())
                .build();
    }

    @Override
    public DishOverViewVO getDishOverViewVO() {
        return DishOverViewVO.builder()
                .sold(dishMapper.getSold())
                .discontinued(dishMapper.getDiscontinued())
                .build();
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {
        Integer allOrders =orderMapper.getAllOrders();//全部订单
        Integer cancelledOrders = orderMapper.getOrdersByStatus(6);
        Integer completedOrders = orderMapper.getOrdersByStatus(5);
        Integer deliveredOrders = orderMapper.getOrdersByStatus(3);
        Integer waitingOrders = orderMapper.getOrdersByStatus(2);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .build();
    }
}
